package com.tenton.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Date: 2021/1/14
 * @Author: Tenton
 * @Description:
 */
@WebServlet(name = "LogoutServlet",urlPatterns = "/logoutServlet")
public class LogoutServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(request.getSession(false)!=null){
            request.getSession().invalidate();
            //删除自动登录 cookie
            Cookie autologinC = new Cookie("autologin", "");
            autologinC.setPath(request.getContextPath());
            autologinC.setMaxAge(0);
            response.addCookie(autologinC);
        }
        response.sendRedirect(request.getContextPath()+"/login.jsp");

    }
}
